package com.ncloud.act.common;

import java.text.DecimalFormat;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ActDataHelper {

	public static String div100(double value) {
		return toStr(value / 100);
	}

	public static String div100(String value) {
		return value == null ? "0.00" : toFormat(div100(toDouble(value)));
	}

	// double 转化成 String 并格式化成0.00
	public static String toStr(double value) {
		return toFormat(Double.toString(value));
	}

	// 数字的String 格式化0.00后输出
	public static String toFormat(Object obj) {
		double value = toDouble(obj);
		DecimalFormat format = new DecimalFormat("0.00");
		return format.format(value);
	}
	
	/*// 截取时间字符串
	public static String getDateTimeString(String dateTime) {
		if(null == dateTime||dateTime.equals(""))
		{
			return "";
		}
		if(dateTime.length()<19)
		{
			return dateTime;
		}
		return "";
	}*/
		
	// 将data里面指定的值除以100
	public static void div100(JSONObject data, String key) {
		if (key == null || key.equals(""))
			key = "0";
		data.put(key, div100(data.getString(key)));
	}

	// 将data里面指定的值除以100
	public static void div100(JSONObject data, String[] keys) {
		if (keys == null || keys.length == 0)
			return;
		for (int i = 0; i < keys.length; i++) {
			data.put(keys[i], div100(data.getString(keys[i])));
		}
	}
	// 将data里面指定的值除以100
	public static void div100(JSONArray datas, String[] keys) {
		if (keys == null || keys.length == 0)
			return;
		for(int i=0;i<datas.size();i++)
		{
			JSONObject data = datas.getJSONObject(i);
			div100(data,keys);
		}
	}
	// String 转化成 double 并进行格式化
	public static double toDouble(Object obj) {
		String value;
		if (obj == null) {
			value = "0.00";
		} else {
			value = obj.toString().trim();
		}
		if (value == null || value.length() == 0)
			return 0.00;
		else
			return (Double.valueOf(value)).doubleValue();
	}
}